package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Name AddressController
 * @Author stella
 * @Date 2023-03-09 2:33 PM
 */
@RestController
@Slf4j
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 地址清单
     * @return
     */
    @GetMapping("/list")
    public ResultInfo list(){
        List<Address> addressList= addressService.list();
        return ResultInfo.success(addressList);
    }

    /**
     * 设置默认地址
     * @param
     * @return
     */
    @PutMapping("/default")
    public ResultInfo defaultAdd(@RequestBody Address address){
        Long id = address.getId();
        addressService.defaultAdd(id);
        return ResultInfo.success();
    }

    /**
     * 新增地址
     * @param address
     * @return
     */
    @PostMapping
    public ResultInfo addAddress(@RequestBody Address address){
        addressService.addAddress(address);
        return ResultInfo.success();
    }

    /**
     * 回显地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResultInfo getAdd(@PathVariable Long id){
        Address address = addressService.getAdd(id);
        return ResultInfo.success(address);
    }

    /**
     * 修改更新地址
     * @param address
     * @return
     */
    @PutMapping
    public ResultInfo updateAdd(@RequestBody Address address){
        addressService.updateAdd(address);
        return ResultInfo.success();
    }

    /**
     * 获取默认地址
     * @return
     */
    @GetMapping("/default")
    public ResultInfo getDefault(){
        Address address = addressService.getDefault();
        return ResultInfo.success(address);
    }
}
